import { saveGridView, Generate, loadReport } from '@/apis/report.js';

export default class ReportInfoManager {
  constructor() {
    this.reportId = -1;
    this.Page = {
      PageWidth: 595, // 报表中页面的默认宽度
      PageHeight: 841, // 报表中页面的默认高度
      InteractiveWidth: 595, // 在交互式呈现器中呈现报表时的页面的默认宽度
      InteractiveHeight: 841, // 在交互式呈现器中呈现报表时的页面的默认高度
      Width: 595, // 报表中页面的默认宽度
      Height: 841, // 报表中页面的默认高度
      Windows: '', //   哪些窗口可挂此报表
      MarginTop: 0, // 报表纸张的边距
      MarginLeft: 0,
      MarginBottom: 0,
      MarginRight: 0,
    };

    // 参数
    this.dataSoruceParameters = [

    ];
    //
    this.DoesDataFromProcedure = true;

    this.ProcedureData = {
      Name: '',
      Tables: [
      // {
      //   Name: '',
      //   Fields: [
      //     { Name: '', ValueType: '' },
      //   ],
      // },
      ],
    };

    // 数据源
    this.allDataSets = [
      // {
      //   Name: '',
      //   SQL: '',
      //   Fields: [
      //     { Name: '', ValueType: '' },
      //   ],
      // },
    ];

    // 报表是否执行套打功能
    this.DoesCustomizePrint = false;
    // 统计报表打印次数的标识:CurrentWindowRecordID
    this.PrintFlag = 'CurrentWindowRecordID';

    // 页眉区域
    // this.TopSection = {
    //   Height: 20,
    //   AllGridViews: [],
    //   Type: 'Top',
    //   PrintOnFirstPage: true,
    //   PrintOnLastPage: true,
    // };
    // 页脚区域
    // this.BottomSection = {
    //   Height: 20,
    //   AllGridViews: [

    //   ],
    //   Type: 'Bottom',
    //   PrintOnFirstPage: true,
    //   PrintOnLastPage: true,
    // };
    // 报表主体样式
    this.Body = {
      Height: 300,
      AllGridViews: [],
      Type: 'Center',
    };
    /// ///////////////////////////////
    this.states = {
      nextTableId: 1,
      tableIndex: -1,
      currentCell: {},
      visibleCells: [], // 所有选中的 可见的单元格 visibleCells<=currentCells
      currentCells: [], // 所有选中的单元格
      ReportViewPropertyVisible: false,
      EditDataSourceVisible: false,
      EditDataSourceParamVisible: false,
      EditTableVisible: false,
      EditExpressionVisible: false,
      EditExpression: '',
    };
    this.expCallBack = null;
  }

  async loadReport(reportId) {
    // 获取存储过程列表
    this.reportId = reportId;
    const res = await loadReport(
      { reportId: this.reportId },
    );
    this.Page.Width = res.report.Width;
    this.Page.PageHeight = res.report.PageHeight;
    this.Page.PageWidth = res.report.PageWidth;
    this.Page.InteractiveWidth = res.report.InteractiveWidth;
    this.Page.InteractiveHeight = res.report.InteractiveHeight;

    this.Page.MarginTop = res.report.MarginTop;
    this.Page.MarginLeft = res.report.MarginLeft;
    this.Page.MarginBottom = res.report.MarginBottom;
    this.Page.MarginRight = res.report.MarginRight;

    this.dataSoruceParameters = res.report.DataSoruceParameters;
    this.allDataSets = res.report.AllDataSets;

    this.Body.Height = res.report.CenterSection.Height;
    this.Body.AllGridViews = res.report.CenterSection.AllGridViews;
    const index = this.Body.AllGridViews.findIndex(a => a.Name.indexOf('cord') > -1);
    this.bookCord = null;
    if (index > -1) {
      this.bookCord = this.Body.AllGridViews.splice(index, 1)[0];
    }

    // 加载完表格后设置 第一个表格为默认表格
    if (this.Body.AllGridViews.length > 0) {
      this.states.tableIndex = 0;
    }

    // 这里需要从服务端获取些数据
    // this.DoesDataFromProcedure = false;
  }

  clearStates() {
    this.states.currentCell = {};
    this.states.currentCells = [];
    this.states.visibleCells = [];
  }

  addNewCell(obj) {
    const cell = {
      BackColor: '#ffffff',
      ForeColor: '#000000',
      CellType: 'Textbox',
      WritingMode: 'Horizontal',
      Text: '',
      TextAlign: 'Center',
      VerticalAlign: 'Middle',
      FontName: '宋体',
      FontSize: 12,
      FontBold: 0,
      FontItalic: 0,
      FontStrikeout: 0,
      FontUnderline: 0,

      PaddingLeft: 2,
      PaddingTop: 2,
      PaddingRight: 2,
      PaddingBottom: 2,

      BorderTopColor: '#000000',
      BorderTopLineStyle: 'None',
      BorderTopWidth: '1',

      BorderLeftColor: '#000000',
      BorderLeftLineStyle: 'None',
      BorderLeftWidth: '1',

      BorderRightColor: '#000000',
      BorderRightLineStyle: 'None',
      BorderRightWidth: '1',

      BorderBottomColor: '#000000',
      BorderBottomLineStyle: 'None',
      BorderBottomWidth: '1',

      ImageName: '',
      // SizingTypeOfImage: 'FitProportional',
      // SourceTypeOfImage: 'Database',
      // NeedCellUnderLine: 'false',
      // CellUnderLineWidth: '1',
      // CellUnderLineStyle: 'Solid',
      // Action: {
      //   Hyperlink: '',
      //   Drillthrough: {
      //     ReportName: '',
      //     ParameterList: '',
      //     ErrorsInDrillthroughType: '',
      //     ReportNameWithExp: '',
      //     DrillthroughIsDynamic: 'false',
      //     ParameterListWithExp: '',
      //   },
      //   ActionType: 'None',
      //   ErrorsInActionType: '',
      // },
      // Format: '',
      // IsSubtotalText: 'false',
      // IsSubtotalRow: 'false',
      // IsSubtotalColumn: 'false',
      // UserSortExpression: '',
      zIndex: 10,
    };
    return Object.assign(cell, obj);
  }

  async saveGridView() {
    // 保存单元格信息
    // 保存主体的单元格信息
    const gridViews = [];
    const saveGrid = JSON.parse(JSON.stringify(this.Body.AllGridViews));
    if (this.bookCord != null) {
      saveGrid.push(this.bookCord);
    }

    saveGrid.forEach(a => {
      const config = [];
      a.Rows.forEach(row => {
        config.push({
          GridViewID: a.ID,
          RowColIndex: row.RowIndex,
          RowColType: row.Type,
          RowColVal: row.Height,
        });
      });
      a.Columns.forEach(col => {
        config.push({
          GridViewID: a.ID,
          RowColIndex: col.ColumnIndex,
          RowColType: '',
          RowColVal: col.Width,
        });
      });
      const gridView = {
        ID: a.ID,
        Name: a.Name,
        dataSourceName: a.dataSourceName || '',
        showType: a.showType,
        GridHidden: a.GridHidden,
        GridHiddenPsm: a.GridHiddenPsm,
        GridHiddenParam: a.GridHiddenParam,
        ViewLeft: a.Left,
        ViewTop: a.Top,
        ViewZIndex: a.ZIndex,
        Configs: config,
        Cells: a.Cells,
      };
      gridViews.push(gridView);
    });

    const res = await saveGridView({
      reportId: this.reportId,
      gridViews: gridViews,
    });
    return res;
  }

  async Generate() {
    const res = await Generate({
      reportId: this.reportId,
    });
    return res;
  }

  /**
   * 删除当前表格
   */
  deleteTable() {
    this.Body.AllGridViews.splice(this.states.tableIndex, 1);
    this.states.tableIndex--;
  }
}
